package com.gin.no_gay_alliance.param;

import android.os.Parcel;
import android.os.Parcelable;

import com.gin.nga.enums.OrderByParam;
import com.gin.nga.enums.SubForumType;
import com.gin.no_gay_alliance.activity.TopicListActivity;
import com.gin.no_gay_alliance.utils.UrlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 展示主题列表的参数，在{@link TopicListActivity}中使用
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/5 14:17
 **/
public class TopicListParam implements Parcelable {
    public static final Creator<TopicListParam> CREATOR = new Creator<TopicListParam>() {
        @Override
        public TopicListParam createFromParcel(Parcel in) {
            return new TopicListParam(in);
        }

        @Override
        public TopicListParam[] newArray(int size) {
            return new TopicListParam[size];
        }
    };
    public Type type;
    /**
     * 页数，仅用于从url解析出的参数
     */
    public Integer page;
    /**
     * 合集id
     */
    public Long colTid;
    /**
     * 从 {@link OrderByParam} 中选择
     */
    public String orderBy;
    /**
     * 收藏夹id
     */
    public Long folderId;
    /**
     * 版面id 多个用逗号隔开
     */
    public String forumId;
    /**
     * 作者uid
     */
    public Long authorUid;
    /**
     * 是否搜索用户的回复
     */
    public boolean searchReply;
    /**
     * 临时标题
     */
    public String title;
    /**
     * 搜索关键字
     */
    public String keyword;
    /**
     * 是否搜索主楼正文
     */
    public Boolean searchContent;
    /**
     * 是否只搜索精华帖
     */
    public Boolean recommendOnly;
    /**
     * 备选的搜索区域
     */
    public List<SearchArea> searchAreas;

    public TopicListParam() {
    }

    public TopicListParam(SubForumType type, long id, String title) {
        this(type, id, id, title);
    }

    public TopicListParam(SubForumType type, Long forumId, Long colTid, String title) {
        if (type == SubForumType.COL) {
            this.type = TopicListParam.Type.COL_LIST;
            this.colTid = colTid;
        }
        if (type == SubForumType.FORUM) {
            this.type = TopicListParam.Type.FORUM_LIST;
            this.forumId = String.valueOf(forumId);
        }
        this.title = title;
    }

    protected TopicListParam(Parcel in) {
        type = in.readParcelable(Type.class.getClassLoader());
        if (in.readByte() == 0) {
            page = null;
        } else {
            page = in.readInt();
        }
        if (in.readByte() == 0) {
            colTid = null;
        } else {
            colTid = in.readLong();
        }
        orderBy = in.readString();
        if (in.readByte() == 0) {
            folderId = null;
        } else {
            folderId = in.readLong();
        }
        forumId = in.readString();
        if (in.readByte() == 0) {
            authorUid = null;
        } else {
            authorUid = in.readLong();
        }
        searchReply = in.readByte() != 0;
        title = in.readString();
        keyword = in.readString();
        byte tmpSearchContent = in.readByte();
        searchContent = tmpSearchContent == 0 ? null : tmpSearchContent == 1;
        byte tmpRecommendOnly = in.readByte();
        recommendOnly = tmpRecommendOnly == 0 ? null : tmpRecommendOnly == 1;
        searchAreas = in.createTypedArrayList(SearchArea.CREATOR);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(type, flags);
        if (page == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(page);
        }
        if (colTid == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(colTid);
        }
        dest.writeString(orderBy);
        if (folderId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(folderId);
        }
        dest.writeString(forumId);
        if (authorUid == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(authorUid);
        }
        dest.writeByte((byte) (searchReply ? 1 : 0));
        dest.writeString(title);
        dest.writeString(keyword);
        dest.writeByte((byte) (searchContent == null ? 0 : searchContent ? 1 : 2));
        dest.writeByte((byte) (recommendOnly == null ? 0 : recommendOnly ? 1 : 2));
        dest.writeTypedList(searchAreas);
    }


    public static TopicListParam userTopic(long userId, boolean recommendOnly, Long forumId) {
        TopicListParam p = new TopicListParam();
        p.type = TopicListParam.Type.USER_SEARCH;
        p.authorUid = userId;
        p.recommendOnly = recommendOnly;
        if (forumId != null) {
            p.forumId = String.valueOf(forumId);
        }
        p.title = recommendOnly ? "用户精华帖" : "用户主题";
        return p;
    }

    public static TopicListParam userReply(long userId, Long forumId) {
        TopicListParam p = new TopicListParam();
        p.type = TopicListParam.Type.USER_SEARCH;
        p.authorUid = userId;
        p.searchReply = true;
        if (forumId != null) {
            p.forumId = String.valueOf(forumId);
        }
        p.title = "用户回复";
        return p;
    }

    /**
     * 从URL中解析生成参数
     *
     * @param url url
     * @return 参数
     */
    public static TopicListParam parseUrl(String url) {
        TopicListParam param = new TopicListParam();
        //query参数
        HashMap<String, List<String>> queryParams = UrlUtils.parseQuery(url);
        for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue().get(0);
            switch (key) {
                case "page":
                    param.page = Integer.valueOf(value);
                    break;
                case "stid":
                    param.colTid = Long.valueOf(value);
                    break;
                case "order_by":
                    param.orderBy = value;
                    break;
                case "favor":
                    param.folderId = Long.valueOf(value);
                    break;
                case "fid":
                    param.forumId = value;
                    break;
                case "authorid":
                    param.authorUid = Long.valueOf(value);
                    break;
                case "searchpost":
                    param.searchReply = "1".equals(value);
                    break;
                case "key":
                    param.keyword = value;
                    break;
                case "content":
                    param.searchContent = true;
                    break;
                case "recommend":
                    param.recommendOnly = "1".equals(value);
                    break;
            }

            // 设置type
            if (param.keyword != null) {
                // 版面搜索或合集搜索
                param.type = param.forumId != null ? Type.FORUM_SEARCH : Type.COL_SEARCH;
            } else if (param.recommendOnly != null && param.recommendOnly) {
                // 精华区
                param.type = Type.FORUM_RECOMMEND;
            } else if (param.authorUid != null) {
                // 搜索用户发言
                param.type = Type.USER_SEARCH;
            } else if (param.folderId != null) {
                param.type = Type.FAVOR;
            } else {
                // 浏览版面或合集
                param.type = param.forumId != null ? Type.FORUM_LIST : Type.COL_LIST;
            }
        }
        return param;
    }

    public Long getId() {
        if (colTid != null) {
            return colTid;
        }
        return forumId == null ? null : Long.parseLong(forumId);
    }

    public SubForumType getSubForumType() {
        if (type == Type.FORUM_LIST || type == Type.FORUM_SEARCH) {
            return SubForumType.FORUM;
        }
        if (type == Type.COL_LIST || type == Type.COL_SEARCH) {
            return SubForumType.COL;
        }
        return null;
    }

    /**
     * 类型
     */
    public enum Type implements Parcelable {
        /**
         * 收藏夹
         */
        FAVOR,
        /**
         * 合集浏览
         */
        COL_LIST,
        /**
         * 合集搜索
         */
        COL_SEARCH,
        /**
         * 版面浏览
         */
        FORUM_LIST,
        /**
         * 版面搜索
         */
        FORUM_SEARCH,
        /**
         * 版面精华
         */
        FORUM_RECOMMEND,
        /**
         * 用户发言
         */
        USER_SEARCH,
        ;

        public static final Creator<Type> CREATOR = new Creator<Type>() {
            @Override
            public Type createFromParcel(Parcel in) {
                return Type.values()[in.readInt()];
            }

            @Override
            public Type[] newArray(int size) {
                return new Type[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(this.ordinal());
        }
    }
}
